﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using System.Data;

namespace ProjectWebNhac.GUI.User
{
    public partial class ChiTietCaSi : System.Web.UI.Page
    {
        int MaCaSi = 1;
        int Pages = 1;
        CaSiBus csBus = new CaSiBus();
        AlbumBus abBus = new AlbumBus();
        BaiHatBus bhBus = new BaiHatBus();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["MaCaSi"]!=null)
            {
                MaCaSi = Convert.ToInt16(Request["MaCaSi"]);

            }
            if (Request["page"] != null)
            {
                Pages = Convert.ToInt16(Request["page"]);
            }
            if (!Page.IsPostBack)
            {
                LoadThongTinCaSi();
                LoadDsAlbum();
                LoadDanhSachBaiHat();
                
            }
        }
        private void LoadThongTinCaSi()
        {
            reThongTin.DataSource= csBus.LayChiTietCasi(MaCaSi);
            Session["TenCaSi"] = csBus.LayChiTietCasi(MaCaSi).
                Rows[0]["TenCaSi"].ToString();
            reThongTin.DataBind();
        }
        private void LoadDsAlbum()
        {
            reAlbum.DataSource = abBus.LayTop4DsAlBumTheoCaSi(MaCaSi);
            reAlbum.DataBind();
        }
        private void LoadDanhSachBaiHat()
        {
            DataSet ds = bhBus.LayDanhSachBaiHatPTTheoCaSi(Pages, 3, 5, MaCaSi);
            DataTable dtbData = ds.Tables[0];
            
            if (dtbData.Rows.Count > 0)
            {
                DataTable dtbPhanTrang = ds.Tables[1];
                reBaiHat.DataSource = dtbData;
                reBaiHat.DataBind();
                liPage.Text = dtbPhanTrang.Rows[0][0].ToString();
            }
        }
        protected void reBaiHat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            CaSiBus csBus = new CaSiBus();
            int MaBaiHat = 0;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView usrCurrent = e.Item.DataItem as DataRowView;
                MaBaiHat = Convert.ToInt16(usrCurrent["MaBaiHat"]);
            }
            if (item.ItemType == ListItemType.Item)
            {
                Repeater reCasi = (Repeater)e.Item.FindControl("reCasi");

                reCasi.DataSource = csBus.LayChiTietCasiTheoBaiHat(MaBaiHat);
                reCasi.DataBind();
            }
        }

        protected void reBaiHat_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "download")
            {
                string linkbai = e.CommandArgument.ToString();
                Response.ContentType = "audio/mp3";
                Response.AddHeader("content-disposition", "attachment; filename=" + linkbai);
                Response.TransmitFile(Server.MapPath("~/Nhac/" + linkbai));
                Response.End();
            }
        }


    }
}